package Factory;

import callback.ListenerCallback;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import sys.Console;

public class ListenerFactory {

    private static Console console = Console.getInstance();

    /**
     * 默认的监听器
     * @param hint
     * @return
     */
    public static ChannelFutureListener getDefaultListener(String hint) {
        return new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()) {
                    console.success(hint);
                } else {
                    Throwable throwable = channelFuture.cause();
                    throwable.printStackTrace();
                }
            }
        };
    };
    public static ChannelFutureListener errorListenter(ListenerCallback callback) {
      return new ChannelFutureListener() {
          @Override
          public void operationComplete(ChannelFuture channelFuture) throws Exception {
              if (channelFuture.isSuccess()) {
                  callback.successCallBack();
              } else {
                  Throwable throwable = channelFuture.cause();
                  callback.errorCallBack(throwable);
              }
          }
      };
    }
}
